//Network Assignment Server part
//Richard Allsopp
//A Simple Java Server

//importing java tools
import java.net.*;
import java.io.*;
import javax.swing.*;       //Import tools
import java.util.*;

public class testServer
{
    public static void main(String[] args) throws IOException
    {
        ServerSocket myServSock = null;
        try
        {   //try block to catch exception if port could not be opened
            myServSock = new ServerSocket(4001);
            System.out.println("Waiting for connection");
        }
        catch (IOException e)
        {   //catching the exception
            System.out.println("Could not listen on port: 40000.");     //outputing the error message
            System.exit(1);   //closing the program
        }
        //setting up a client socket
        Socket myCliSock = null;
        try
        {   //try block to catch failier to connect exception
            myCliSock = myServSock.accept();
            System.out.println("Connection received from " + myCliSock.getInetAddress().getHostName());
        }
        catch (IOException e)
        {
            System.out.println("Accept failed.");       //outputting error message
            System.exit(1);         //closing the program
        }
        //Setting up a pritwriter to handle the sending of strings
        PrintWriter byteOut = new PrintWriter(myCliSock.getOutputStream(), true);
        BufferedReader byteIn = new BufferedReader( new InputStreamReader( myCliSock.getInputStream()));
        String message;
        String serverMessage = "1|SR|Hello|%";
        String serverMessageNot = "0|SR|Not Registered|%";


        File file = new File("ebooks.txt");
        if(!file.exists())
        {
            JOptionPane.showMessageDialog(null, "The File does not Exists", "Alert", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }

        while ((message = byteIn.readLine()) != null)
        {   //keeping the connection open till someone types terminate
            System.out.println(message);
            char test = message.charAt(4);
            System.out.println(test);
            if(message.charAt(4) == 'r')
            {
                byteOut.println(serverMessage);
            }
            else if(message.charAt(4) == 'b')
            {
                byteOut.println(serverMessageNot);
            }
            else if(message.charAt(4) == 'A')
            {   
                Scanner newFile = new Scanner(new FileInputStream("ebooks.txt"));   //setup scanner object for both files
                while (newFile.hasNext())     //run while loop to add file output to TreeSet oldAccounts
                {
                    String s1 = newFile.nextLine();
                    byteOut.println(s1);
                }
            }
            else if (message.equals("Terminate"))
            {
                break;
            }
        }

        byteIn.close();
        myCliSock.close();
        myServSock.close();
        //closing the connections
    }
}
//end of server
